package com.jtjrenren.android.taxi.passenger.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.support.multidex.MultiDex;

import com.anbang.pay.sdk.app.BangfubaoApplication;
import com.baidu.mapapi.SDKInitializer;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.service.PushMainService;
import com.jtjrenren.android.taxi.passenger.manger.service.PushMsgService;
import com.jtjrenren.android.taxi.passenger.utils.InitView;
import com.igexin.sdk.PushManager;
import com.library.utils.AppException;
import com.library.utils.AppUtils;
import com.library.utils.LogUtil;
import com.liulishuo.filedownloader.FileDownloader;


public class BaseApplication extends BangfubaoApplication {
    private static BaseApplication instance;

    public static BaseApplication getInstance() {
        return instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this) ;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        if (AppUtils.isProcessRunning(getApplicationContext())) {
            instance = this;
            init();
        }

        startPush();

        FileDownloader.init(getApplicationContext());

        addListeners();
    }

    /**
     * 初始化必要的配置
     */
    private void init() {
        //初始化异常处理
        AppException.getInstance().init(getApplicationContext());

        //初始化百度地图
        SDKInitializer.initialize(this);

        //初始化imageloader
        InitView.initImageLoader(getApplicationContext());

        //初始化住逻辑类
        AppManger.getInstance().init(getApplicationContext());
    }

    /**
     * 个推开启
     */
    public void startPush() {
        LogUtil.log("push", "个推初始化");
        PushManager.getInstance().initialize(this.getApplicationContext(), PushMainService.class);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), PushMsgService.class);
    }

    private int activityCount = 0;//activity的count数
    private boolean isForeground = true;//是否在前台

    private void addListeners() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {
                if (!isForeground) {
                    LogUtil.log("push", "程序从后台切换回前台，重新初始化个推");
                    startPush();
                }
                isForeground = true;
                activityCount++;
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                activityCount--;
                if (0 == activityCount) {
                    isForeground = false;
                }
                LogUtil.log("app", "当前程序是否在前台：" + isForeground);
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }
}
